﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DarkCRM.Helpers;
using System.Collections.Specialized;
using System.Data;


namespace DarkCRM.Models
{
    public class Member:Entity
    {

        public int MemberID { get; set; }
        public string DisplayName { get; set; }
        public string Email { get; set; }
        public string Password { get; set; }
        public bool Sex { get; set; }
        public int RoleID { get; set; }
        public int Department { get; set; }
        public DateTime CreateDate { get; set; }
        public DateTime LastLoginDate { get; set; }
        public DateTime UpdateDate { get; set; }
        public bool IsEnable { get; set; }
        public bool ViewOnly { get; set; }
        public int CreateBy { get; set; }
        public bool IsDelete { get; set; }

        public Member()
        {

        }

        public static Member GetCookieAdmin()
        {
            Member admin = new Member() { DisplayName = "", Email = "", MemberID = 0, RoleID = 0 };
            HttpCookie cookie = HttpContext.Current.Request.Cookies["darkbo"];
            if (cookie == null)
            {
                return admin;
            }
            else
            {
                try
                {
                    //displayname={0}&memberid={1}&roleid={2}
                    string value = SecurityHelper.DiscuzAuthcodeDecode(cookie.Value, "t2tdarkbo");
                    NameValueCollection nv = System.Web.HttpUtility.ParseQueryString(value);
                    string name = nv["displayname"];
                    int memberid = int.Parse(nv["memberid"]);
                    //string email = nv["email"];
                    int roleID = int.Parse(nv["roleid"]);

                    admin.DisplayName = name;
                    //admin.Email = email;
                    admin.MemberID = memberid;
                    admin.RoleID = roleID;

                    return admin;

                }
                catch (Exception)
                {
                    return admin;

                }
            }
        }


        [DarkAjax.Utility.jQueryInvokeMethod(Url = "handlers/AjaxController.ashx", Method = "post", DataType = "json", Async = true)]
        public static ResultMessage Login(string name, string pwd)
        {
            try
            {
                name = name.Replace("'", "");
                pwd = pwd.Replace("'", "");

                //string connectionString = GenHelper.GenConnectionString();
                //string commond = string.Format("select w_level from [admin] where w_name='{0}' and w_pass='{1}'", name, pwd);
                //object roleid = Helpers.AccessHelper.ExecuteScalar(connectionString, commond);

                IDictionary<string, object> par = new Dictionary<string, object>();
                par.Add("DisplayName", name);
                par.Add("Password", pwd);
                object member = SqlHelper.ExecuteReader("GetMemberByNameNPwd", par);
                //bool isExits= Helpers.AccessHelper.ExecuteScalar(connectionString, commond) != null;
                if (member != null)
                {
                    DataTable dt = ((DataSet)member).Tables[0];
                    Member m = new Member();
                    m.Load(dt.Rows[0]);
                    string cookieValue = string.Format("displayname={0}&memberid={1}&roleid={2}", m.DisplayName , m.MemberID, m.RoleID);
                    HttpContext.Current.Session["name"] = cookieValue;
                    cookieValue = SecurityHelper.DiscuzAuthcodeEncode(cookieValue, "t2tdarkbo");

                    HttpCookie cookie = new HttpCookie("darkbo", cookieValue);
                    HttpContext.Current.Response.Cookies.Add(cookie);

                    return new ResultMessage() { ErrorCode = 0, Description = "seccess" };
                }
                else
                {
                    return new ResultMessage() { ErrorCode = 1, Description = "用户名或密码错误！" };
                }
            }
            catch (Exception e)
            {
                return new ResultMessage() { ErrorCode = 1, Description = e.Message };
            }


        }
    }
}